package com.ruoyi.web.controller.demo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.DemoProduct;
import com.ruoyi.demo.service.base.DemoProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 产品Controller
 * 
 * @author zhangshu
 * @date 2021-01-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/demo/product" )
public class DemoProductController extends BaseController {

    private final DemoProductService iDemoProductService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('demo:product:list')")
    @GetMapping("/list")
    public AjaxResult list(DemoProduct demoProduct) {
        List<DemoProduct> list = iDemoProductService.queryList(demoProduct);
        return AjaxResult.success(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('demo:product:export')" )
    @Log(title = "产品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(DemoProduct demoProduct) {
        List<DemoProduct> list = iDemoProductService.queryList(demoProduct);
        ExcelUtil<DemoProduct> util = new ExcelUtil<DemoProduct>(DemoProduct.class);
        return util.exportExcel(list, "product" );
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:product:query')" )
    @GetMapping(value = "/{productId}" )
    public AjaxResult getInfo(@PathVariable("productId" ) Long productId) {
        return AjaxResult.success(iDemoProductService.getById(productId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('demo:product:add')" )
    @Log(title = "产品" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoProduct demoProduct) {
        return toAjax(iDemoProductService.save(demoProduct) ? 1 : 0);
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('demo:product:edit')" )
    @Log(title = "产品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoProduct demoProduct) {
        return toAjax(iDemoProductService.updateById(demoProduct) ? 1 : 0);
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('demo:product:remove')" )
    @Log(title = "产品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}" )
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(iDemoProductService.removeByIds(Arrays.asList(productIds)) ? 1 : 0);
    }
}
